// LSL script generated: RLV-New.RLV_MENU.DV_RLV_ForceSit.lslp Sun May  2 00:09:51 Westeuropäische Sommerzeit 2010
//21   B  Force sit on an object : @sit:<UUID>=force (*) 


integer attachment = 1;
string cmdname;
key victim;
list gMenu = [];
list statesMenu = [0,0,0,0,0,0,0,0];
key ToucherID;

integer channel_dialog;
integer listen_id;
integer listen_id2;
string msg = "Force Sit";
integer gPage = 0;
list objectKey = [];
list pageMenu(list in,integer page){
    list out;
    integer maxpages = (llGetListLength(in) / 9);
    (page = (page % (maxpages + 1)));
    (out = ["<<","[Back]",">>"]);
    integer _listcount = ((page * 9) + 9);
    integer _i = (_listcount - 9);
    for (_i; (_i < _listcount); (_i++)) {
        if (((_i <= llGetListLength(in)) && (llList2String(in,_i) != ""))) (out += llList2String(in,_i));
    }
    return out;
}

llRlvSay(string command){
    if ((attachment == 1)) {
        llOwnerSay(("@" + command));
    }
    else  {
        llSay(-1812221819,(((((cmdname + ",") + ((string)victim)) + ",") + "@") + command));
    }
}

makeForceSit(){
    llRlvSay("getsitid=13337");
    (listen_id2 = llListen(13337,"",NULL_KEY,""));
}

displayMenu(){
    list _menu;
    list _manipulation;
    if ((llGetListLength(gMenu) < 10)) {
        (_menu = ([" ","[Back]"," "] + gMenu));
    }
    else  {
        (_menu = pageMenu(gMenu,gPage));
    }
    (channel_dialog = (-1 * ((integer)("0x" + llGetSubString(((string)llGetKey()),-5,-1)))));
    (listen_id = llListen(channel_dialog,"",NULL_KEY,""));
    llDialog(ToucherID,msg,_menu,channel_dialog);
    llSetTimerEvent(45.0);
}

default {

    state_entry() {
    }

    
    listen(integer channel,string name,key id,string message) {
        if ((channel == channel_dialog)) {
            llListenRemove(listen_id);
            llSetTimerEvent(0.0);
            if ((message == "<<")) {
                (gPage--);
                integer maxpages = (llGetListLength(gMenu) / 9);
                if ((gPage < 0)) {
                    (gPage = maxpages);
                }
                displayMenu();
            }
            else  if ((message == ">>")) {
                (gPage++);
                displayMenu();
            }
            else  if ((message == "[Back]")) llMessageLinked(-4,1337,"DV_MAIN_RLV",ToucherID);
            else  if ((message == "Unsit")) {
                llRlvSay("unsit=force");
                llMessageLinked(-4,1337,"DV_MAIN_RLV",ToucherID);
            }
            else  if ((~llListFindList(gMenu,((list)message)))) {
                string _objectKey = llList2String(objectKey,((integer)message));
                llRlvSay((("sit:" + _objectKey) + "=force"));
                llMessageLinked(-4,1337,"DV_MAIN_RLV",ToucherID);
            }
        }
        if ((channel == 13337)) {
            llListenRemove(listen_id2);
            llSetTimerEvent(0.0);
            if ((message == NULL_KEY)) {
                llSensor("",NULL_KEY,8,19,6.28318548);
            }
            else  {
                (msg = "Avatar Sit's");
                (gMenu = ["Unsit"]);
                displayMenu();
            }
        }
    }

    
    on_rez(integer on) {
    }

    sensor(integer num) {
        integer n;
        (gMenu = []);
        (objectKey = []);
        (msg = "");
        for ((n = 0); (n < num); (++n)) {
            (objectKey += llDetectedKey(n));
            (msg += (((((string)n) + ": ") + llDetectedName(n)) + "\n"));
            (gMenu += ((string)n));
        }
        displayMenu();
    }

    link_message(integer sender,integer num,string str,key id) {
        if ((num == 1337)) {
            if ((str == llGetScriptName())) {
                llMessageLinked(-4,1338,llGetScriptName(),NULL_KEY);
                (ToucherID = id);
                makeForceSit();
            }
        }
        if ((num == 1338)) {
            if ((str == "attachment")) (attachment = 1);
            else  if ((str == "device")) (attachment = 0);
            else  if ((str != llGetScriptName())) {
                llListenRemove(listen_id);
                llListenRemove(listen_id2);
            }
        }
        if ((num == 1339)) {
            if ((str == "RESET")) {
                (statesMenu = [0,0,0,0,0,0,0,0]);
                (objectKey = []);
            }
        }
        if ((num == 1329)) {
            (victim = ((key)str));
            (cmdname = ((string)llGetKey()));
        }
    }

  	timer() {
        llListenRemove(listen_id);
        llListenRemove(listen_id2);
    }
}
